home *** CD-ROM | disk | FTP | other *** search
- /*
- GetTLE - MemoDebug.c - routines to write debug output to a Memo
- Copyright ⌐2000 Andreas Schneider
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- #ifdef linux
- #include <stdarg.h>
- #include <stdio.h>
- #include <string.h>
- #else
- #include <PalmOS.h>
- #include <unix_stdarg.h> // that's not what the documentation says...
- #define vsprintf StrVPrintF
- #define strcat StrCat
- #endif
-
- #include "debug.h"
-
- #ifdef DEBUGGING // no need to have code we don't use
-
- // Structures imposed by other programs
-
- typedef struct {
- char note; // null terminated string
- } MemoDBRecordType;
-
- typedef MemoDBRecordType * MemoDBRecordPtr;
-
- #define MemoDBName "MemoDB"
- #define MemoDBType 'DATA'
-
- #define MAX_DEBUG_LENGTH 4000 // limit output
-
- #ifndef linux
- // we use one large buffer. Messages get appended to it and
- // only at the very end is the information stored in the Memo
- static char debug_message[MAX_DEBUG_LENGTH]="";
- #endif
-
- // a switch to turn logging on and off
- static short debugging_enabled=0;
- // and a way to find out at the end if we need to create a memo
- static short got_messages=0;
-
- // append a text message to the DebugMessage[] buffer
- extern void LogMessage(char *format,...)
- {
- va_list args;
-
- if (debugging_enabled)
- {
- va_start(args,format);
- got_messages=1;
- VLogMessage(format,args);
- va_end(args);
- }
- return;
- }
-
- extern void VLogMessage(char *format,va_list args)
- {
- char message[150]=""; // seems a reasonable limit - I'll not do checks
- unsigned long length;
- #ifndef linux
- static unsigned long total_length=1; // count the trailing 0 right from the start
- #endif
-
- // vsprintf returns length of string
- length=vsprintf(message,format,args);
- #ifdef linux
- fputs(message,stderr);
- #else
- // make sure we don't overrun the buffer
- if (total_length+length<MAX_DEBUG_LENGTH)
- {
- // no overrun problems - append message to buffer
- strcat(debug_message,message);
- // and keep track of total size of buffer so far
- total_length+=length;
- }
- #endif
- return;
- }
-
- // must be called first
- extern void InitDebugMessages(void)
- {
- #ifndef linux
- UInt32 Seconds;
- DateTimeType DateTime;
- char Message[64]="";
- char DateString[dateStringLength];
- char TimeString[timeStringLength];
-
- // get the current time
- Seconds=TimGetSeconds();
- // and convert it to a proper Date/Time combination
- TimSecondsToDateTime(Seconds,&DateTime);
- // Create the Memo title
- // Start with the word 'DEbug'
- StrCat(Message,"Debug ");
- // then append the date
- DateToAscii(DateTime.month,DateTime.day,DateTime.year,dfDMYWithDashes,DateString);
- StrCat(Message,DateString);
- // and finally the time
- StrCat(Message," ");
- TimeToAscii(DateTime.hour,DateTime.minute,tfColon24h,TimeString);
- StrCat(Message,TimeString);
- // terminate with a new line
- StrCat(Message,"\n");
- // and store it as the first message in DebugMessage[]
- StrCopy(debug_message,Message);
- #endif
- return;
- }
-
- // We also want a way to start and stop debug messages
- extern void StartDebugMessages(void)
- {
- debugging_enabled=true;
- }
-
- extern void StopDebugMessages(void)
- {
- debugging_enabled=true;;
- }
-
- // when we're finished with the program we write the debug
- // information to a Memo
- extern void WriteDebugMessages(void)
- {
- #ifndef linux
- DmOpenRef MemoDB=0;
- UInt16 index=1000;
- MemHandle recordHandle;
- MemoDBRecordPtr recordPtr;
- Err result;
- UInt32 length;
-
- // open the database of the built-in Memo application
- MemoDB = DmOpenDatabaseByTypeCreator(MemoDBType, 'memo', dmModeReadWrite);
- if (MemoDB && got_messages) // only create a new memo if there are messages
- {
- // success - allocate memory for the new memo
- length=(UInt32)StrLen(debug_message);
- recordHandle=(MemHandle)DmNewHandle(MemoDB,(UInt32)(length+1)); // +1 - the trailin 0
- if (recordHandle!=NULL)
- {
- // copy the DebugMessage[] into the new record
- recordPtr=MemHandleLock(recordHandle);
- DmStrCopy(recordPtr,0,debug_message);
- MemPtrUnlock(recordPtr);
- // and attach the record to the database
- result=DmAttachRecord(MemoDB,&index,recordHandle,0);
- if (result)
- {
- // we can now free the record Handle
- MemHandleFree(recordHandle);
- }
- }
- else
- {
- // We could display "Can't get new handle" here, but we don't
- }
- }
- else
- {
- // "Can't find memo database";
- }
- #endif
- return;
- }
-
- #else
-
- // because of the varargs I'll need an empty implementation of LogMessage
-
- extern void LogMessage(char *format,...)
- {
- return;
- }
- #endif // DEBUGGING
-